package com.lenovo.smartengine.utils;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.dolby.api.DsAccessException;
import com.dolby.api.DsGlobalEx;

public class AudioTypeSetUtils {
    public static long firstStartServiceTime = 0;

    public static int queryAudioTypeFromWaves(Context mContext) {
        int media_type = -1;
        int enable = -1;
        int maxxsense = -1;
        int index = -1;
        ContentResolver contentResolver = mContext.getContentResolver();
        Cursor cursor = contentResolver.query(Constants.CONFIG_URI,
                new String[] { Constants.COLUMN_NAME_ENABLE,
                        Constants.COLUMN_NAME_MEDIA_TYPE,
                        Constants.COLUME_NAME_MAXXSENSE }, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            enable = Integer.parseInt(cursor.getString(cursor
                    .getColumnIndex(Constants.COLUMN_NAME_ENABLE)));
            try {
                index = cursor.getColumnIndex(Constants.COLUME_NAME_MAXXSENSE);
            } catch (Exception e) {
                Loge(Constants.TAG,
                        "Waves ContentProvider Error :" + e.getMessage());
            }
            if (enable > 0) {
                if (index >= 0) {
                    maxxsense = Integer.parseInt(cursor.getString(index));
                    Logi(Constants.TAG, "maxxsense=" + maxxsense);
                    if (maxxsense == Constants.AUTOBUTTONISON) {
                        media_type = Integer
                                .parseInt(cursor.getString(cursor
                                        .getColumnIndex(Constants.COLUMN_NAME_MEDIA_TYPE)));
                    } else {
                        Logi(Constants.TAG, "Waves AutoEffect is off...");
                    }
                } else {
                    media_type = Integer.parseInt(cursor.getString(cursor
                            .getColumnIndex(Constants.COLUMN_NAME_MEDIA_TYPE)));
                }
            } else {
                Logi(Constants.TAG, "Waves is off...");
            }
        }
        return media_type;
    }

    public static boolean isWavesApp(Context mContext) {
        boolean isWaves = false;
        ContentResolver contentResolver = mContext.getContentResolver();
        Cursor cursor = null;
        try {
            cursor = contentResolver.query(Constants.CONFIG_URI,
                    new String[] { Constants.COLUMN_NAME_MEDIA_TYPE }, null,
                    null, null);
        } catch (Exception e) {
            Loge(Constants.TAG, "Waves query error:" + e.getMessage());
        }
        if (cursor != null) {
            isWaves = true;
        }
        return isWaves;
    }

    public static void setAudioTypeToWaves(Context mContext, int mState) {
        Logi(Constants.TAG, "setAudioTypeToWaves...setProfile: " + mState);
        ContentResolver contentResolver = mContext.getContentResolver();
        ContentValues values = new ContentValues();
        boolean isDisable = mState < 3;
        if (isDisable) {
            values.put(Constants.COLUMN_NAME_MEDIA_TYPE, mState);// music:0,video:1,game:2
            try {
                int result = contentResolver.update(Constants.CONFIG_URI,
                        values, null, null);
                if (result > 0) {
                    Logi(Constants.TAG, "Waves setAudioType success...");
                } else {
                    Logi(Constants.TAG, "Waves setAudioType failed...");
                }
            } catch (Exception e) {
                Loge(Constants.TAG, "Waves ContentResolver Error!");
            }
        } else {
            Logi(Constants.TAG, "Waves's audiotype is out of range");
        }
    }

    public static int queryAudioTypeFromDolby(DsGlobalEx mDsClient) {
        int profile = -1;
        if (null != mDsClient) {
            if (mDsClient.getState() == 1)
                profile = mDsClient.getProfile();// music:1,video:0,systemAudio:2
        }
        return profile;
    }

    public static void setAudioTypeToDolby(DsGlobalEx mDsClient, int profile) {
        Logi(Constants.TAG, "setAudioTypeToDolby...setProfile:" + profile);
        if (mDsClient != null) {
            try {
                int result = mDsClient.setProfile(profile);
                if (result == 0) {
                    Logi(Constants.TAG, "Dolby setAudioType success...");
                } else {
                    Logi(Constants.TAG, "Dolby setAudioType failed...");
                }
            } catch (DsAccessException e) {
                e.printStackTrace();
            }
        }
    }

    public static boolean isDolbyApp(DsGlobalEx mDsClient) {
        boolean isDolby = false;
        if (mDsClient != null) {
            String version = mDsClient.getDsVersion();
            if (version != null && version.indexOf("DAX") >= 0) {
                isDolby = true;
            }
        }
        return isDolby;
    }

    // smart engine to switch media type by the broadcasted media type value
    public static void smartEngineToSwitchMediaTypeForWaves(Context context,
            int phonemediatype, int type_value, long broadcast_received,
            long broadcast_start) {
        // phonemediatype not as type_value,because it will be modified by
        // manual during listening to music
        if ((phonemediatype != type_value)
                && (type_value >= Constants.BASE_TYPE)) {
            AudioTypeSetUtils.setAudioTypeToWaves(context, type_value);
            long now = System.currentTimeMillis();
            Logi(Constants.TAG,
                    "Waves ,audio effect switching...time consumed:"
                            + (now - broadcast_received) + "ms");
            Logi(Constants.TAG, "Waves ,the whole process...time consumed:"
                    + (now - broadcast_start) + "ms");
        }
    }

    public static void smartEngineToSwitchMediaTypeForDolby(Context context,
            DsGlobalEx mDsClient, int phonemediatype, int type_value,
            long broadcast_received, long broadcast_start) {
        if (mDsClient == null) {
            Logi(Constants.TAG, "mDsClient==null");
        } else if ((phonemediatype != type_value)
                && (type_value >= Constants.BASE_TYPE)) {
            AudioTypeSetUtils.setAudioTypeToDolby(mDsClient, type_value);
            long now = System.currentTimeMillis();
            Logi(Constants.TAG,
                    "Dolby ,audio effect switching...time consumed:"
                            + (now - broadcast_received) + "ms");
            Logi(Constants.TAG, "Dolby ,the whole process...time consumed:"
                    + (now - broadcast_start) + "ms");
        }
    }

    public static void Logi(String tag, String msg) {
        if (Log.isLoggable(tag, Log.VERBOSE)) {
            Log.i(tag, msg);
        }
    }

    public static void Loge(String tag, String msg) {
        if (Log.isLoggable(tag, Log.VERBOSE)) {
            Log.e(tag, msg);
        }
    }
}
